package com.example.testproject.ext

import android.content.Context
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.testproject.R
import com.example.testproject.adapter.SelectDepartmentAdapter
import com.example.testproject.bean.SelectBean
import com.example.testproject.template.SelectDepartmentTemplate
import com.example.testproject.utils.SwitchDialogFragment

/**
 *
 * @ClassName: ContextExt
 * @Author: android 超级兵
 * @CreateDate: 10/21/21$ 2:23 PM$
 * TODO Context 扩展
 */

fun Context.toDip(value: Float) = let {
    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, this.resources.displayMetrics)
}



/*
 * 作者:android 超级兵
 * 创建时间: 12/20/21 9:35 AM
 * TODO 选择部门dialog
 */

// 当前数据
private var currentData = ArrayList<SelectBean.Data>()

fun Context.showSelectDialog(
    data: ArrayList<SelectBean.Data>, // 必须传参数
    onSuccessCallBack: (SelectBean.Data) -> Unit, // 确定回调
) = let {
    val view = LayoutInflater.from(this).inflate(R.layout.select_dialog_layout, null)

    val tempList = ArrayList<List<SelectBean.Data>>()

    val adapter: SelectDepartmentAdapter
    view.findViewById<RecyclerView>(R.id.selectRv).also {
        SelectDepartmentTemplate(
            tempList
        ).apply {
            template(this@showSelectDialog, it).apply {
                adapter = getAdapter() as SelectDepartmentAdapter
                adapter.addData(listOf(data, data[0].childs))
            }
        }
    }

    var tempItem = data[0]

    // 确定回调
    val tvSuccess = view.findViewById<TextView>(R.id.tvSuccess)
    // 取消按钮
    val tvCancel = view.findViewById<TextView>(R.id.tvCancel)
    // 选择部门按钮
    val tvSelect = view.findViewById<TextView>(R.id.tvSelect)

    adapter.onParentClick =
        SelectDepartmentAdapter.OnSelectDepartmentParentListener { parentPosition, _, item ->
            tempItem = item
            tvSelect.text = item.name
            currentData = item.childs
            // 单选
            adapter.data.forEachIndexed { i, parentData ->
                parentData.forEach {
                    if (parentPosition <= i) {
                        it.isSelect = false
                    }
                }
            }
            item.isSelect = true
            adapter.notifyDataSetChanged()

            // adapter.data.size - 2 = 保留2位元素
            val tempAdapterSize = adapter.data.size - 2

            // 如果点击的 是5      size = 7 那么只保留5条数据
            if (parentPosition < tempAdapterSize) {
                repeat(tempAdapterSize - parentPosition) {
                    adapter.data.removeAt(adapter.data.size - 1)
                }
            }
            if (currentData.isNotEmpty()) {
                if (parentPosition == adapter.data.size - 1) {
                    //   adapter.data.size - 1 = 当前集合长度
                    adapter.addData(currentData)
                } else {
                    adapter.setData(parentPosition + 1, currentData)
                }
            }
        }

    SwitchDialogFragment(view, "", this).also {
        it.setBottomShow(true)
        it.setLeftVisibility(true)
        it.setRightVisibility(true)

        tvCancel.click { _ -> it.dismiss() }
        tvSuccess.click { _ ->
            onSuccessCallBack(tempItem)
            it.dismiss()
        }
    }
}